<!-- eslint-disable vue/valid-v-slot -->
<template>
  <div class="main-container">
    <TableBody>
      <template #header>
        <TableHeader ref="tableHeaderRef" :show-filter="false">
          <template #search-content>
            <a-form ref="serchForm" :model="form">
              <a-row :gutter="24">
                <a-col :span="4">
                  <a-form-item field="tradeNo" hide-label>
                    <a-input v-model="form.tradeNo" placeholder="请输入订单号" />
                  </a-form-item>
                </a-col>
                <!-- <a-col :span="4">
                  <a-form-item field="userIds" hide-label>
                    <a-select
                      v-model="form.userId"
                      placeholder="请选择客户"
                      :max-tag-count="1"
                      allow-clear
                      allow-search
                      :options="userList"
                      :virtual-list-props="{ height: 200 }"
                    />
                  </a-form-item>
                </a-col> -->
                <a-col :span="4">
                  <a-form-item field="isGeneralStock" hide-label>
                    <a-select
                      v-model="form.isGeneralStock"
                      placeholder="请选择库存类型"
                      allow-clear
                    >
                      <a-option :value="true">通用库存</a-option>
                      <a-option :value="false">商品库存</a-option>
                    </a-select>
                  </a-form-item>
                </a-col>
                <a-col :span="4">
                  <a-form-item field="type" hide-label>
                    <a-select v-model="form.type" placeholder="请选择交易类型" allow-clear>
                      <a-option v-for="item in typeList" :key="item.key" :value="item.key">{{
                        item.value
                      }}</a-option>
                    </a-select>
                  </a-form-item>
                </a-col>
                <a-col :span="4">
                  <a-form-item field="add" hide-label>
                    <a-select v-model="form.add" placeholder="请选择类型" allow-clear>
                      <a-option :value="true">增加</a-option>
                      <a-option :value="false">减少</a-option>
                    </a-select>
                  </a-form-item>
                </a-col>
                <a-col :span="4">
                  <a-form-item field="addTime" hide-label>
                    <a-date-picker
                      v-model="form.addTime"
                      style="width: 100%"
                      show-time
                      placeholder="请选择交易开始时间"
                      :shortcuts="[
                        {
                          label: '今日',
                          value: () => (
                            (form.addTime = $dayjs('00:00:00', 'HH:mm:ss').format(
                              'YYYY-MM-DD HH:mm:ss'
                            )),
                            (form.endTime = $dayjs('23:59:59', 'HH:mm:ss').format(
                              'YYYY-MM-DD HH:mm:ss'
                            ))
                          ),
                        },
                        {
                          label: '昨日',
                          value: () => (
                            (form.addTime = $dayjs('00:00:00', 'HH:mm:ss')
                              .subtract(1, 'day')
                              .format('YYYY-MM-DD HH:mm:ss')),
                            (form.endTime = $dayjs('23:59:59', 'HH:mm:ss')
                              .subtract(1, 'day')
                              .format('YYYY-MM-DD HH:mm:ss'))
                          ),
                        },
                        {
                          label: '七日',
                          value: () => (
                            (form.addTime = $dayjs('00:00:00', 'HH:mm:ss')
                              .subtract(6, 'day')
                              .format('YYYY-MM-DD HH:mm:ss')),
                            (form.endTime = $dayjs().format('YYYY-MM-DD HH:mm:ss'))
                          ),
                        },
                        {
                          label: '本月',
                          value: () => (
                            (form.addTime = $dayjs('01 00:00:00', 'DD HH:mm:ss').format(
                              'YYYY-MM-DD HH:mm:ss'
                            )),
                            (form.endTime = $dayjs().format('YYYY-MM-DD HH:mm:ss'))
                          ),
                        },
                        {
                          label: '上月',
                          value: () => (
                            (form.addTime = $dayjs(`01 00:00:00`, 'DD HH:mm:ss')
                              .subtract(1, 'month')
                              .format('YYYY-MM-DD HH:mm:ss')),
                            (form.endTime = $dayjs()
                              .subtract(1, 'month')
                              .endOf('month')
                              .format('YYYY-MM-DD HH:mm:ss'))
                          ),
                        },
                      ]"
                      :time-picker-props="{ defaultValue: '12:00:00' }"
                      format="YYYY-MM-DD HH:mm:ss"
                      allow-clear
                    />
                  </a-form-item>
                </a-col>
                <a-col :span="4">
                  <a-form-item field="endTime" hide-label>
                    <a-date-picker
                      v-model="form.endTime"
                      style="width: 100%"
                      show-time
                      placeholder="请选择交易结束时间"
                      :shortcuts="[
                        {
                          label: '今日',
                          value: () => (
                            (form.addTime = $dayjs('00:00:00', 'HH:mm:ss').format(
                              'YYYY-MM-DD HH:mm:ss'
                            )),
                            (form.endTime = $dayjs('23:59:59', 'HH:mm:ss').format(
                              'YYYY-MM-DD HH:mm:ss'
                            ))
                          ),
                        },
                        {
                          label: '昨日',
                          value: () => (
                            (form.addTime = $dayjs('00:00:00', 'HH:mm:ss')
                              .subtract(1, 'day')
                              .format('YYYY-MM-DD HH:mm:ss')),
                            (form.endTime = $dayjs('23:59:59', 'HH:mm:ss')
                              .subtract(1, 'day')
                              .format('YYYY-MM-DD HH:mm:ss'))
                          ),
                        },
                        {
                          label: '七日',
                          value: () => (
                            (form.addTime = $dayjs('00:00:00', 'HH:mm:ss')
                              .subtract(6, 'day')
                              .format('YYYY-MM-DD HH:mm:ss')),
                            (form.endTime = $dayjs().format('YYYY-MM-DD HH:mm:ss'))
                          ),
                        },
                        {
                          label: '本月',
                          value: () => (
                            (form.addTime = $dayjs('01 00:00:00', 'DD HH:mm:ss').format(
                              'YYYY-MM-DD HH:mm:ss'
                            )),
                            (form.endTime = $dayjs().format('YYYY-MM-DD HH:mm:ss'))
                          ),
                        },
                        {
                          label: '上月',
                          value: () => (
                            (form.addTime = $dayjs(`01 00:00:00`, 'DD HH:mm:ss')
                              .subtract(1, 'month')
                              .format('YYYY-MM-DD HH:mm:ss')),
                            (form.endTime = $dayjs()
                              .subtract(1, 'month')
                              .endOf('month')
                              .format('YYYY-MM-DD HH:mm:ss'))
                          ),
                        },
                      ]"
                      :time-picker-props="{ defaultValue: '12:00:00' }"
                      format="YYYY-MM-DD HH:mm:ss"
                      allow-clear
                    />
                  </a-form-item>
                </a-col>
                <a-col :span="4">
                  <a-form-item field="addNumber" hide-label>
                    <a-input-group style="width: 100%">
                      <a-input v-model="form.addNumber" placeholder="开始数量" allow-clear />
                      <a-input v-model="form.endNumber" placeholder="结束数量" allow-clear />
                    </a-input-group>
                  </a-form-item>
                </a-col>
                <a-col :span="4">
                  <a-form-item field="addNumber" hide-label>
                    <a-input-group style="width: 100%">
                      <a-input
                        v-model="form.addAddNumber"
                        placeholder="开始操作前数量"
                        allow-clear
                      />
                      <a-input
                        v-model="form.endAddNumber"
                        placeholder="结束操作前数量"
                        allow-clear
                      />
                    </a-input-group>
                  </a-form-item>
                </a-col>
                <a-col :span="4">
                  <a-form-item field="addEndNumber" hide-label>
                    <a-input-group style="width: 100%">
                      <a-input
                        v-model="form.addEndNumber"
                        placeholder="开始操作后数量"
                        allow-clear
                      />
                      <a-input
                        v-model="form.endEndNumber"
                        placeholder="结束操作后数量"
                        allow-clear
                      />
                    </a-input-group>
                  </a-form-item>
                </a-col>
                <a-col :span="4">
                  <a-form-item field="btns" hide-label>
                    <a-space :size="15">
                      <a-button
                        type="primary"
                        v-permission="billCard"
                        html-type="submit"
                        @click="doRefresh(1)"
                        ><template #icon>
                          <icon-search />
                        </template>
                        <!-- Use the default slot to avoid extra spaces -->
                        <template #default>搜索</template></a-button
                      >
                      <a-button v-permission="billCard" @click="resetSeach"
                        ><template #icon>
                          <icon-sync theme="outline" size="14" />
                        </template>
                        <!-- Use the default slot to avoid extra spaces -->
                        <template #default>重置</template></a-button
                      >
                    </a-space>
                  </a-form-item>
                </a-col>
              </a-row>
            </a-form>
          </template>
        </TableHeader>
      </template>
      <template #table-config>
        <a-space :size="15">
          <a-button
            type="outline"
            v-permission="exportBillCard"
            @click="goodsExport"
            :loading="loading"
          >
            <!-- Use the default slot to avoid extra spaces -->
            <template #default>导出</template>
          </a-button>
        </a-space>
        <a-space :size="15">
          <TableConfig
            @update-border="onUpdateBorder"
            @update-striped="onUpdateStriped"
            @refresh="doRefresh"
          />
        </a-space>
      </template>
      <template #default>
        <a-table
          :bordered="{ wrapper: true, cell: bordered }"
          :stripe="(striped as boolean)"
          :loading="tableLoading"
          :data="list"
          :pagination="false"
          :column-resizable="true"
          :rowKey="rowKey"
          :columns="tableColumns.list"
          :scrollbar="true"
          :scroll="{ x: 1000 }"
          table-layout-fixed
        >
          <template #columns>
            <a-table-column
              v-for="item of tableColumns.list"
              :key="item.key"
              align="left"
              :width="item.width"
              :ellipsis="true"
              :tooltip="true"
              :title="(item.title as string)"
              :data-index="(item.key as string)"
              :fixed="item.fixed"
            >
              <template v-if="item.key === 'remarks'" #cell="{ record }">
                <span v-if="record.remarks">{{ record.remarks }}</span>
                <span v-else>---</span>
              </template>
              <template v-else-if="item.key === 'user'" #cell="{ record }">
                {{ record.user.userName }}
              </template>
              <template v-else-if="item.key === 'isGeneralStock'" #cell="{ record }">
                {{ record.isGeneralStock ? '通用库存' : '商品库存' }}
              </template>

              <template v-else-if="item.key === 'type'" #cell="{ record }">
                <span
                  v-for="(items, index) in typeList"
                  :key="index"
                  v-show="items.key == record.type"
                  >{{ items.value }}</span
                >
              </template>
            </a-table-column>
          </template>
        </a-table>
      </template>
      <template #footer>
        <TableFooter ref="tableFooterRef" :pagination="pagination" />
      </template>
    </TableBody>
  </div>
</template>

<script lang="ts" setup>
  import { get } from '@/api/http'
  import { billCard, exportBillCard } from '@/api/url'
  import useCommonStore from '@/store/modules/common'
  import { usePagination, useRowKey, useTable } from '@/hooks/table'
  import { onMounted, reactive, ref } from 'vue'
  //导出商品列表
  const goodsExport = () => {
    userCommon
      .downLoads('/app/mall/stock/details/user/get/list/export', form, '库存明细')
      .then(() => {})
      .catch(() => {})
  }
  const form = reactive({
    tradeNo: '', //        订单号
    userId: '', //          代理ID
    isGeneralStock: '', //  是否是通用库存
    type: '', //            类型
    addNumber: '', //       开始数量
    endNumber: '', //       结束数量
    addTime: '', //         开始时间
    endTime: '', //         结束时间
    add: '', //            是否增加
    addAddNumber: '', //   开始操作前数量
    endAddNumber: '', //   结束操作前数量
    addEndNumber: '', //   开始操作后数量
    endEndNumber: '', //   结束操作后数量
  })

  const userCommon = useCommonStore()
  const typeLists: any = ref([])
  const table = useTable()
  const rowKey = useRowKey('id')
  const pagination = usePagination(doRefresh)

  const tableColumns: any = reactive({
    list: [
      {
        title: 'ID',
        key: 'id',
        dataIndex: 'id',
        checked: true,
        width: 113,
      },
      {
        title: '客户账号',
        key: 'user',
        dataIndex: 'user',
        checked: true,
        width: 113,
      },
      {
        title: '操作前库存',
        key: 'addNumber',
        dataIndex: 'addNumber',
        checked: true,
        width: 120,
      },
      {
        title: '操作库存',
        key: 'number',
        dataIndex: 'number',
        checked: true,
        width: 120,
      },
      {
        title: '操作后库存',
        key: 'endNumber',
        dataIndex: 'endNumber',
        checked: true,
        width: 120,
      },
      {
        title: '库存类型',
        key: 'isGeneralStock',
        dataIndex: 'isGeneralStock',
        checked: true,
        width: 120,
      },
      {
        title: '交易类型',
        key: 'type',
        dataIndex: 'type',
        checked: true,
        width: 120,
      },
      {
        title: '交易备注',
        key: 'remarks',
        dataIndex: 'remarks',
        checked: true,
        width: 120,
      },
      {
        title: '交易时间',
        key: 'addTime',
        dataIndex: 'addTime',
        checked: true,
        width: 190,
      },
    ],
  })

  const tableLoading = ref(true)

  const loading = ref(false)
  const list = ref([])
  const card = ref('')

  const exportLists = () => {
    loading.value = true
    userCommon.downLoadPosts(exportBillCard, form, '卡板明细列表').then(() => {
      loading.value = false
    })
  }

  function doRefresh(value?: number) {
    // eslint-disable-next-line prettier/prettier
    tableLoading.value = true
    if (value) {
      pagination.page = value
    }
    get({
      url: '/app/mall/stock/details/user/get/list',
      data: () => {
        return {
          page: pagination.page,
          size: pagination.pageSize,
          ...form,
        }
      },
    })
      .then((res) => {
        tableLoading.value = false
        list.value = res.data.list
        pagination.setTotalSize((res.data as any).totalSize)
      })
      .catch(console.log)
  }

  function onUpdateBorder(isBordered: boolean) {
    table.bordered.value = isBordered
  }
  function onUpdateStriped(isStriped: boolean) {
    table.striped.value = isStriped
  }

  const serchForm = ref()
  const resetSeach = () => {
    card.value = ''
    serchForm.value?.resetFields()
    form.addAddNumber = ''
    form.endAddNumber = ''
    form.addNumber = ''
    form.endNumber = ''
    form.addEndNumber = ''
    form.endEndNumber = ''
    doRefresh(1)
  }
  const userList: any = ref([])
  //客户
  function getList() {
    userCommon.userListse().then((res: any) => {
      userList.value = res.data
    })
  }
  const typeList: any = ref([])
  function getType() {
    get({
      url: '/app/mall/stock/details/type',
    }).then((res) => {
      typeList.value = res.data
    })
  }
  onMounted(async () => {
    doRefresh()
    getList()
    getType()
  })
</script>
<style lang="less" scoped>
  :deep(.arco-modal) {
    .arco-modal-footer {
      text-align: right !important;
    }
  }
  :deep(.arco-modal-header .arco-modal-title) {
    display: flex;
    justify-content: flex-start !important;
  }
</style>
